package com.yiguantong.police.ui.utils;


import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.text.TextUtils;

import com.MainApplication;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @description: 工具类
 * @date: 2017/9/15
 * @version:
 */
public class Util {

    private static PackageInfo packInfo;
    private static String DEVICEKEY = "";

    /**
     * 方法名称：getVersionName
     * 方法描述：获取系统的版本名称
     */
    public static String getVersionName() {
        if (packInfo == null) {
            getAppInfo();
        }
        return packInfo != null ? packInfo.versionName : "";
    }

    /**
     * getApkDataDir：获取apk包名路径
     */
    public static String getApkDataDir() {
        if (Build.VERSION.SDK_INT != 23 && packInfo == null)
            getAppInfo();
        return packInfo != null && packInfo.applicationInfo != null ? packInfo.applicationInfo.dataDir : "";
    }

    /**
     * 获取当前版本号
     *
     * @return
     */
    public static Serializable getVersionCode() {
        if (packInfo == null)
            getAppInfo();
        return packInfo != null ? packInfo.versionCode : "0";
    }

    private static void getAppInfo() {
        // 获取packageManager的实例
        PackageManager packageManager = MainApplication.getIns().getPackageManager();
        try {
            packInfo = packageManager.getPackageInfo(MainApplication.getIns().getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 验证手机格式
     */
    public static boolean isValidMobile(String mobiles) {
        String telRegex = "[1]\\d{10}";
        if (TextUtils.isEmpty(mobiles))
            return false;
        else
            return mobiles.matches(telRegex);
    }


    public static boolean isValidPhoneNum(String mobile) {
        Pattern p = Pattern.compile("^[1]\\d{10}");
        Matcher phone = p.matcher(mobile);
        if (TextUtils.isEmpty(mobile)) {
            MainApplication.getIns().tip("手机号不能为空");
            return false;
        } else if (!phone.matches()) {
            MainApplication.getIns().tip("您输入的手机号不是正规手机号，请重新输入！");
            return false;
        } else
            return true;
    }

    /**
     * 可变的UUID
     */
    public static String getRandomUUID() {
        UUID uuid = UUID.randomUUID();
        String uniqueId = uuid.toString();
        return uniqueId.replaceAll("-", "").substring(0, 16);
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    public static String getCurrentTime() {
        Date date = new Date();// 获取当前日期对象
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置格式
        String createTime = format.format(date);
        return createTime;
    }

    /**
     * 品牌
     *
     * @return 品牌
     */
    public static String getDeviceBrand() {
        return Build.BRAND;
    }
}